<template>
  <div id="globalHeader">
    <a-row>
      <a-col flex="120px">
        <router-link to="/">
          <div class="title-bar">
            <img src="../assets/logo.jpeg" width="32" alt="logo" class="logo" />
            <div class="title">云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="100px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id != 0">
            {{  loginUserStore.loginUser.userName ?? '无名'}}
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { h, ref } from 'vue'
import { HomeOutlined } from '@ant-design/icons-vue'
import { MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'

const loginUserStore = useLoginUserStore()
//高亮
const current = ref<string[]>([])
const items = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/about',
    label: '关于',
    title: '关于',
  },
  {
    key: 'others',
    label: h('a', { href: 'https://ant.design', target: '_blank' }, 'Ant Design'),
    title: '其它',
  },
])
const router = useRouter()
//路由点击事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key
  })
}
//路由切换时根据路径设置高亮菜单
router.afterEach((to, from, failure) => {
  current.value = [to.path]
})

</script>

<style scoped>
#globalHeader .title-bar {
  /*水平居中*/
  display: flex;
  /*垂直居中*/
  align-items: center;

  float: left;
}

.logo {
  width: 48px;
  height: 48px;
}

.title {
  font-size: 20px;
  font-weight: bold;
  margin-left: 10px;
  color: black;
}
</style>
